package lcof;

import java.util.Stack;

/**
 * @author 江岸
 * @version V1.0
 * @ClassName: MinStack30
 * @description: 定义栈的数据结构，请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中，调用 min、push 及 pop 的时间复杂度都是 O(1)。
 * https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/
 * @date 2021/3/1715:13
 * @since V1.0
 */
public class MinStack30 {



    private Stack<int[]> stack;

    /**
     * initialize your data structure here.
     */
    public MinStack30() {
        stack = new Stack<>();
    }

    public void push(int x) {
        int[] ints = new int[2];
        ints[0] = x;
        //数组0存数字，1存当前最小值
        if (stack.isEmpty()){
            ints[1] = x;
        }else {
            ints[1] = Math.min(x,stack.peek()[1]);
        }
        stack.push(ints);
    }

    public void pop() {
        stack.pop();
    }

    public int top() {
        return stack.peek()[0];
    }

    public int min() {
        return stack.peek()[1];
    }

}
